package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.weixing.mall.provider.mapper.RoleMapper;
import com.weixing.mall.provider.mapper.RoleMenuMapper;
import com.weixing.mall.provider.mapper.RoleUserMapper;
import com.weixing.mall.provider.model.domain.Role;
import com.weixing.mall.provider.model.domain.RoleMenu;
import com.weixing.mall.provider.model.domain.RoleUser;
import com.weixing.mall.provider.model.dto.sys.SysRoleDto;
import com.weixing.mall.provider.model.enums.sys.SysFlag;
import com.weixing.mall.provider.model.query.SysRoleQuery;
import com.weixing.mall.provider.model.vo.sys.SysRoleEditVo;
import com.weixing.mall.provider.model.vo.sys.SysRoleFormVo;
import com.weixing.mall.provider.model.vo.sys.SysRoleVo;
import com.weixing.mall.provider.service.IRoleMenuService;
import com.weixing.mall.provider.service.IRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {


    @Autowired
    private RoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private RoleUserMapper sysRoleUserMapper;

    @Autowired
    private IRoleMenuService roleMenuService;

    @Override
    public IPage<SysRoleVo> selectOfPage(Page page, SysRoleQuery sysRoleQuery) {
        return baseMapper.selectOfPage(page, sysRoleQuery);
    }

    @Override
    public Role selectByRoleName(String roleName, Integer sysFlag) {
        return baseMapper.selectOne(new QueryWrapper<Role>()
                .eq("role_name", roleName)
                .eq("sys_flag", sysFlag));
    }

    @Override
    public Role selectByRoleCode(String roleCode, Integer sysFlag) {
        return baseMapper.selectOne(new QueryWrapper<Role>()
                .eq("role_code", roleCode)
                .eq("sys_flag", sysFlag));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SysRoleDto sysRoleDto) {
        Role sysRole = new Role();
        BeanUtils.copyProperties(sysRoleDto, sysRole);
        sysRole.setIsEnable(sysRoleDto.getIsEnable().getValue());
        sysRole.setDataGroup(sysRoleDto.getDataGroup().getValue());
        if (null == sysRoleDto.getId()) {
            save(sysRole);
        } else {
            updateById(sysRole);
        }
        Long roleid = sysRole.getId();
        //1、先删除原有的角色记录
        sysRoleMenuMapper.deleteByRoleId(roleid);
        //2、保存新的角色记录
        Long[] menus = sysRoleDto.getMenus();
        ArrayList<RoleMenu> list = Lists.newArrayList();
        if (menus.length > 0) {
            RoleMenu sysRoleMenu;
            for (Long menuId : menus) {
                sysRoleMenu = new RoleMenu();
                sysRoleMenu.setSysFlag(sysRoleDto.getSysFlag());
                sysRoleMenu.setRoleId(roleid);
                sysRoleMenu.setMenuId(menuId);
                list.add(sysRoleMenu);
            }
        }
        roleMenuService.saveBatch(list);
    }


    @Override
    public boolean checkRoleUsed(long id) {
        List<RoleUser> list = sysRoleUserMapper.selectByRoleId(id);
        return list.isEmpty();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(long id) {
        // 1、删除角色与菜单对应关系
        sysRoleMenuMapper.deleteByRoleId(id);
        // 2、删除角色与用户关联关系
        sysRoleUserMapper.selectByRoleId(id);
        // 3、删除角色
        removeById(id);
    }

    @Override
    public SysRoleEditVo selectSysRoleEditVoById(long id) {

        SysRoleEditVo sysRoleEditVo = baseMapper.selectSysRoleVoById(id);
        List<RoleMenu> sysRoleMenus = sysRoleMenuMapper.selectByRoleId(id);
        ArrayList<Long> menus = Lists.newArrayList();
        if (!sysRoleMenus.isEmpty()) {
            int size = sysRoleMenus.size();
            for (int i = 0; i < size; i++) {
                menus.add(sysRoleMenus.get(i).getMenuId());
            }
        }
        sysRoleEditVo.setMenus(menus);
        return sysRoleEditVo;

    }

    @Override
    public List<SysRoleFormVo> selectSysRoleFormVoList(SysFlag sysFlag) {
        return baseMapper.selectSysRoleFormVoList(sysFlag.getValue());
    }

    @Override
    public List<Role> selectSysRoleByUserId(long userId, SysFlag sysFlag) {
        return baseMapper.selectSysRoleByUserId(userId,sysFlag.getValue());
    }

    @Override
    public List<SysRoleVo> selectSysAll() {
        return baseMapper.selectSysAll();
    }
}
